package com.utf999.qiangmanger.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.utf999.qiangmanger.controller.permission.AuthorityAnnotation;
import com.utf999.qiangmanger.controller.permission.AuthorityScope;
import com.utf999.qiangmanger.controller.permission.PromptModeType;
import com.utf999.qiangmanger.entity.JsonEntity;
import com.utf999.qiangmanger.entity.orm.AdminUserEntity;
import com.utf999.qiangmanger.entity.orm.StoreUserEntity;
import com.utf999.qiangmanger.entity.orm.StoreUserGroupEntity;
import com.utf999.qiangmanger.exception.BizException;
import com.utf999.qiangmanger.exception.ResponseCode;
import com.utf999.qiangmanger.service.iface.IStoreUserGroupService;
import com.utf999.qiangmanger.service.iface.IStoreUserService;

/**
 * 店面用户管理页面和操作控制。<br>
 * 请注意管理用户和店面用户的区别。
 * @author yinwenjie
 */
@Controller
@RequestMapping(value={"/storeuser"})
public class StoreUserController extends BaseController {
	
	private static Log controllerLog4j = LogFactory.getLog(StoreUserController.class);
	
	@Autowired
	private IStoreUserService storeUserService;
	
	@Autowired
	private IStoreUserGroupService storeUserGroupService;
	
	/**
	 * 创建一个管理员自定义的用户组。
	 * @param request
	 * @param response
	 * @param storeid 对应的门店编号信息（这样可以找到对应的公司id）
	 * @param groupName 自定义用户分组的名称
	 * @param groupRemark 可能的用户组描述信息
	 */
	@RequestMapping(value={"/createStoreUserGroup"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin } , promptMode=PromptModeType.AJAX)
	public void createStoreUserGroup(HttpServletRequest request , HttpServletResponse response , String companyid , String groupName , String groupRemark) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.storeUserGroupService.createStoreUserGroup(companyid, groupName, groupRemark);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 为指定的店面创建一个服务员（用户）。
	 * @param request
	 * @param response
	 * @param storeid 指定的店面编号（店面id）
	 * @param realName 服务员的真实姓名，必须填写
	 * @param workNumber 服务员的工号
	 * @param userName 服务员的用户名（唯一，如果重复则报错）
	 * @param password 服务员的密码
	 */
	@RequestMapping(value={"/createStoreUser"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin , AuthorityScope.StoreAdmin } , promptMode=PromptModeType.AJAX)
	public void createStoreUser(HttpServletRequest request , HttpServletResponse response , String storeid , String realName , String workNumber , String userName , String password) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.storeUserService.createStoreUser(storeid, realName, workNumber, userName, password);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 对指定的用户自定义分组进行修改。注意只能修改分组名和分组描述；系统定义的默认分组是不允许修改的。
	 * @param request
	 * @param response
	 * @param groupid 指定的用户自定义分组
	 * @param groupName 新的分组名
	 * @param groupRemark 新的分组说明（可以没有）
	 */
	@RequestMapping(value={"/modifyStoreUserGroup"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin } , promptMode=PromptModeType.AJAX)
	public void modifyStoreUserGroup(HttpServletRequest request , HttpServletResponse response , String groupid , String groupName , String groupRemark) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.storeUserGroupService.modifyStoreUserGroup(groupid, groupName, groupRemark);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 修改指定的用户自定义分组的状态。如果是正常则改为禁用；如果是禁用则改为正常
	 * @param request
	 * @param response
	 * @param groupid 指定的用户自定义分组
	 */
	@RequestMapping(value={"/modifyUserGroupStatus"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin , AuthorityScope.StoreAdmin } , promptMode=PromptModeType.AJAX)
	public void modifyUserGroupStatus(HttpServletRequest request , HttpServletResponse response , String groupid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.storeUserGroupService.modifyUserGroupStatus(groupid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 管理员对指定用户的密码进行修改
	 * @param request
	 * @param response
	 * @param userid 指定的用户编号
	 * @param password 新的密码
	 */
	@RequestMapping(value={"/modifyUserPassword"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin , AuthorityScope.StoreAdmin } , promptMode=PromptModeType.AJAX)
	public void modifyUserPassword(HttpServletRequest request , HttpServletResponse response , String userid , String password) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.storeUserService.modifyUserPassword(userid, password);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 修改用户的状态
	 * @param request
	 * @param response
	 * @param adminid 指定的用户id
	 */
	@RequestMapping(value={"/modifyUserStatus"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin ,AuthorityScope.StoreAdmin} , promptMode=PromptModeType.AJAX)
	public void modifyUserStatus(HttpServletRequest request , HttpServletResponse response , String userid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		AdminUserEntity user = null;
		try {
			this.storeUserService.modifyUserStatus(userid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(user);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 将一个指定的用户添加到一个指定的用户组中。<br>
	 * 注意，如果这个用户已经在这个用户组中了，系统将会报错。
	 * @param request
	 * @param response
	 * @param userid 指定的用户编号
	 * @param groupids 指定的用户分组编号(可以只有一个，可以有多个，但是不能没有)
	 * @throws BizException
	 */
	@RequestMapping(value={"/bindUsergroupMapping"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void bindUsergroupMapping(HttpServletRequest request , HttpServletResponse response , String userid , String... groupids) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.storeUserGroupService.bindUsergroupMapping(userid, groupids);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 将一个指定的用户和一个指定的用户组解除绑定关系。
	 * @param request
	 * @param response
	 * @param userid 指定的用户编号
	 * @param groupids 指定的用户分组编号(可以只有一个，可以有多个，但是不能没有)
	 * @throws BizException
	 */
	@RequestMapping(value={"/unbindUsergroupMapping"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin } , promptMode=PromptModeType.AJAX)
	public void unbindUsergroupMapping(HttpServletRequest request , HttpServletResponse response , String userid , String... groupids) throws BizException {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.storeUserGroupService.unbindUsergroupMapping(userid, groupids);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 准备进入“店面服务员”列表显示页面时的数据准备阶段
	 * @param request
	 * @param response
	 * @return 跳转到“店面服务员”列表显示页面
	 * @throws BizException
	 */
	@RequestMapping(value={"/preStoreUserList"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin , AuthorityScope.StoreAdmin } , promptMode=PromptModeType.REDIRECT)
	public ModelAndView preStoreUserList(HttpServletRequest request , HttpServletResponse response) throws BizException {
		ModelAndView mv = new ModelAndView("configuration/userlist");
		return mv;
	}
	
	/**
	 * 准备进入“店面服务员”列表显示页面时的数据准备阶段
	 * @param request
	 * @param response
	 * @return 跳转到“店面服务员”列表显示页面
	 * @throws BizException
	 */
	@RequestMapping(value={"/preStoreUserGroupList"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.REDIRECT)
	public ModelAndView preStoreUserGroupList(HttpServletRequest request , HttpServletResponse response) throws BizException {
		ModelAndView mv = new ModelAndView("configuration/userGrouplist");
		return mv;
	}
	
	/**
	 * 通过ajax的方式，根据公司id查询指定的用户组信息（包括了系统用户组和管理员创建的用户组）
	 * @param request
	 * @param response
	 * @param storeid 指定的店面id
	 */
	@RequestMapping(value={"/queryStoregroupByCompanyid"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryStoregroupByCompanyid(HttpServletRequest request , HttpServletResponse response , String companyid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		List<StoreUserGroupEntity> groups = null;
		try {
			groups = this.storeUserGroupService.queryStoregroupByCompanyid(companyid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(groups);
			this.writeResponseMsg(response, jsonEntity , new String[]{"company"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} catch (Exception e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(ResponseCode.E501);
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} 
	}
	
	/**
	 * 查询指定的用户已经属于了哪些可用的用户组信息
	 * @param request
	 * @param response
	 * @param userid 店面用户组编号
	 */
	@RequestMapping(value={"/queryStroegroupByUserid"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin } , promptMode=PromptModeType.AJAX)
	public void queryStroegroupByUserid(HttpServletRequest request , HttpServletResponse response , String userid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		List<StoreUserGroupEntity> groups = null;
		try {
			groups = this.storeUserGroupService.queryStroegroupByUserid(userid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(groups);
			this.writeResponseMsg(response, jsonEntity , new String[]{"storefront","company"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} catch (Exception e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(ResponseCode.E501);
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} 
	}
	
	/**
	 * 查询指定的工单类型已经属于了哪些可用的用户组信息
	 * @param request
	 * @param response
	 * @param userid 店面用户组编号
	 */
	@RequestMapping(value={"/queryStroegroupByOrderTypeid"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryStroegroupByOrderTypeid(HttpServletRequest request , HttpServletResponse response , String ordertypeid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		List<StoreUserGroupEntity> groups = null;
		try {
			groups = this.storeUserGroupService.queryStroegroupByOrderTypeid(ordertypeid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(groups);
			this.writeResponseMsg(response, jsonEntity , new String[]{"storefront","company"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} catch (Exception e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(ResponseCode.E501);
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} 
	}
	
	/**
	 * 根据用户组id查询用户组的详细信息
	 * @param request
	 * @param response
	 * @param groupid
	 */
	@RequestMapping(value={"/queryStoreUsergroup"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin } , promptMode=PromptModeType.AJAX)
	public void queryStoreUsergroup(HttpServletRequest request , HttpServletResponse response , String groupid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		StoreUserGroupEntity group = null;
		try {
			group = (StoreUserGroupEntity)this.storeUserGroupService.getEntity(groupid);
			//这种情况下不能进行查询
			if(group.getGroupType() == 1) {
				throw new BizException("不能对系统用户组信息进行查询！", ResponseCode.E501);
			}
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(group);
			this.writeResponseMsg(response, jsonEntity , new String[]{"storefront","company"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 根据指定的店面编号和员工工号，查询指定的app用户
	 * @param request
	 * @param response
	 * @param storeid 指定的店面编号
	 * @param workNumber  指定的app用户工号
	 */
	@RequestMapping(value={"/queryStoreuserByWorknumber"})
	@AuthorityAnnotation(scope={AuthorityScope.StoreAdmin, AuthorityScope.CompanyAdmin } , promptMode=PromptModeType.AJAX)
	public void queryStoreuserByWorknumber(HttpServletRequest request , HttpServletResponse response , String storeid, String workNumber) {
		JsonEntity jsonEntity = new JsonEntity();
		
		StoreUserEntity user = null;
		try {
			user = this.storeUserService.queryStoreuserByWorknumber(storeid, workNumber);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(user);
			this.writeResponseMsg(response, jsonEntity , new String[]{"storefront","company","groups"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 按照店面id查询该店面的用户信息（无论这个用户的状态）
	 * @param request
	 * @param response
	 * @param storeid 指定的店面id
	 */
	@RequestMapping(value={"/queryStoreuserByStoreid"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin , AuthorityScope.StoreAdmin } , promptMode=PromptModeType.AJAX)
	public void queryStoreuserByStoreid(HttpServletRequest request , HttpServletResponse response , String storeid, String workNumber , String userName , String realName) {
		JsonEntity jsonEntity = new JsonEntity();
		
		List<StoreUserEntity> users = null;
		try {
			users = this.storeUserService.queryStoreuserByStoreid(storeid, workNumber , userName , realName);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(users);
			this.writeResponseMsg(response, jsonEntity , new String[]{"storefront","company"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 查询指定的店面用户名是否被使用。<br>
	 * 如果存在，则data=true；其他情况返回false
	 * @param request 
	 * @param response 
	 * @param userName 需要查询存在性的用户名
	 */
	@RequestMapping(value={"/exsitStoreuser"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin , AuthorityScope.StoreAdmin } , promptMode=PromptModeType.AJAX)
	public void exsitStoreuser(HttpServletRequest request , HttpServletResponse response , String userName) {
		JsonEntity jsonEntity = new JsonEntity();
		
		StoreUserEntity user = null;
		try {
			user = this.storeUserService.queryStoreuserByUsername(userName);
			if(user == null) {
				jsonEntity.setData(false);
			} else {
				jsonEntity.setData(true);
			}
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 查询指定的店面用户名是否被使用。（通过员工工号查询）<br>
	 * 如果存在，则data=true；其他情况返回false
	 * @param request 
	 * @param response 
	 * @param userName 需要查询存在性的用户名
	 */
	@RequestMapping(value={"/exsitStoreuserByWorkNumber"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin , AuthorityScope.StoreAdmin } , promptMode=PromptModeType.AJAX)
	public void exsitStoreuserByWorkNumber(HttpServletRequest request , HttpServletResponse response , String storeid, String workNumber) {
		JsonEntity jsonEntity = new JsonEntity();
		
		StoreUserEntity user = null;
		try {
			user = this.storeUserService.queryStoreuserByWorknumber(storeid, workNumber);
			if(user == null) {
				jsonEntity.setData(false);
			} else {
				jsonEntity.setData(true);
			}
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
}
